package org.niit.dao

import org.niit.util.JDBCUtil
import java.sql.Timestamp
import java.util.Date

/**
 * 推荐系统DAO，用于将推荐结果写入数据库
 */
object RecommendDao {

  /**
   * 保存热门品类推荐结果
   *
   * @param category 菜品类别
   * @param dishName 菜品名称
   * @param orderCount 订单数量
   * @param score 评分
   */
  def saveHotCategory(category: String, dishName: String, orderCount: Int, score: Double): Unit = {
    clearRecommendTable("recommend_hot_category")
    val sql = "INSERT INTO recommend_hot_category (category, dish_name, order_count, score, update_time) VALUES (?, ?, ?, ?, ?)"
    val params = Array(category, dishName, orderCount, score, new Timestamp(new Date().getTime))
    JDBCUtil.executeUpdate(sql, params)
  }

  /**
   * 保存热门商铺推荐结果
   *
   * @param merchantName 商铺名称
   * @param orderCount 订单数量
   * @param score 评分
   */
  def saveHotMerchant(merchantName: String, orderCount: Int, score: Double): Unit = {
    clearRecommendTable("recommend_hot_merchant")
    val sql = "INSERT INTO recommend_hot_merchant (merchant_name, order_count, score, update_time) VALUES (?, ?, ?, ?)"
    val params = Array(merchantName, orderCount, score, new Timestamp(new Date().getTime))
    JDBCUtil.executeUpdate(sql, params)
  }

  /**
   * 保存热门平台推荐结果
   *
   * @param platform 平台
   * @param orderCount 订单数量
   * @param score 评分
   */
  def saveHotPlatform(platform: String, orderCount: Int, score: Double): Unit = {
    clearRecommendTable("recommend_hot_platform")
    val sql = "INSERT INTO recommend_hot_platform (platform, order_count, score, update_time) VALUES (?, ?, ?, ?)"
    val params = Array(platform, orderCount, score, new Timestamp(new Date().getTime))
    JDBCUtil.executeUpdate(sql, params)
  }

  /**
   * 保存基于时段的推荐结果
   *
   * @param timePeriod 时间段
   * @param dishName 菜品名称
   * @param category 菜品类别
   * @param score 评分
   */
  def saveTimeBasedRecommend(timePeriod: String, dishName: String, category: String, score: Double): Unit = {
    clearRecommendTable("recommend_time_based")
    val sql = "INSERT INTO recommend_time_based (time_period, dish_name, category, score, update_time) VALUES (?, ?, ?, ?, ?)"
    val params = Array(timePeriod, dishName, category, score, new Timestamp(new Date().getTime))
    JDBCUtil.executeUpdate(sql, params)
  }

  /**
   * 保存用户个性化推荐结果
   *
   * @param userId 用户ID
   * @param dishName 菜品名称
   * @param category 菜品类别
   * @param merchantName 商铺名称
   * @param score 评分
   */
  def savePersonalizedRecommend(userId: String, dishName: String, category: String, merchantName: String, score: Double): Unit = {
    clearRecommendTable("recommend_personalized")
    val sql = "INSERT INTO recommend_personalized (user_id, dish_name, category, merchant_name, score, update_time) VALUES (?, ?, ?, ?, ?, ?)"
    val params = Array(userId, dishName, category, merchantName, score, new Timestamp(new Date().getTime))
    JDBCUtil.executeUpdate(sql, params)
  }

  /**
   * 批量保存推荐结果
   *
   * @param tableName 表名
   * @param columns 列名
   * @param data 数据列表，每个元素是一行数据
   */
  def batchSaveRecommendResult(tableName: String, columns: String, data: List[Array[Any]]): Unit = {
    clearRecommendTable(tableName)
    val sql = s"INSERT INTO $tableName ($columns) VALUES (${("?" * data.head.length).split("").mkString(", ")})"
    JDBCUtil.executeBatchUpdate(sql, data)
  }

  /**
   * 清空推荐表以准备新的批次数据
   *
   * @param tableName 表名
   */
  def clearRecommendTable(tableName: String): Unit = {
    val sql = s"DELETE FROM $tableName"
    JDBCUtil.executeUpdate(sql, Array())
  }
} 